package gui;

import java.awt.EventQueue;
import java.awt.TextArea;

import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.BorderLayout;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import java.awt.event.ActionListener;
import java.util.Iterator;
import java.util.LinkedList;
import java.awt.event.ActionEvent;

public class guitext {

	private JFrame frame;
	private JTextField textField;
	private JTextArea textArea;
	private JButton btnNewButton_1, btnNewButton_2;
	private LinkedList<Dictionary> list = new LinkedList<Dictionary>();
	private boolean B = false; // 判断中英互换
	private int n = 2;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					guitext window = new guitext();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public guitext() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame("翻译(默认中译英)");
		frame.setBounds(100, 100, 450, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);

		JButton btnNewButton = new JButton("查询");// 按钮
		// 创建词典内容
		Dictionary Entry[] = new Dictionary[n];

		Entry[0] = new Dictionary("hello", "你好");
		Entry[1] = new Dictionary("world", "世界");

		for (int i = 0; i < n; i++) {
			list.add(Entry[i]);
		}

		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 默认中译英
				String edit;
				Dictionary d;
				boolean bool = false; // 有无找到
				if (B && e.getSource() == btnNewButton) {
					// 获取编辑框内容
					String regex = "[a-zA-Z]+";
					edit = textField.getText();
					if (!edit.matches(regex)) {
						textArea.setText("输入正确 的英文字符");
						return;
					}
					edit = edit.toLowerCase();// 转换为小写
					// 匹配
					Iterator<Dictionary> it = list.iterator();
					while (it.hasNext()) {
						d = it.next();
						// 英译中
						if (edit.equals(d.English)) {
							textArea.setText(d.Chinese);
							bool = true;

						}
					}
				}

				if (!B && e.getSource() == btnNewButton) {
					// 获取编辑框内容
					String regex = "[\u4e00-\u9fa5]+";
					edit = textField.getText();
					if (!edit.matches(regex)) {
						textArea.setText("输入正确中文");
						return;
					}
					// 匹配
					Iterator<Dictionary> it = list.iterator();
					while (it.hasNext()) {
						d = it.next();
						// 中译英
						if (edit.equals(d.Chinese)) {
							textArea.setText(d.English);
							bool = true;

						}
					}
				}
				if (!bool) {
					textArea.setText("没有找到该词");

				}
			}
		});
		btnNewButton.setBounds(308, 79, 68, 27);
		frame.getContentPane().add(btnNewButton);

		textField = new JTextField();
		textField.setBounds(57, 79, 186, 27);
		frame.getContentPane().add(textField);
		textField.setColumns(10);

		textArea = new JTextArea();
		textArea.setBounds(57, 134, 210, 95);
		frame.getContentPane().add(textArea);

		btnNewButton_1 = new JButton("中译英");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				B = false;

			}
		});
		btnNewButton_1.setBounds(29, 11, 113, 27);
		frame.getContentPane().add(btnNewButton_1);

		btnNewButton_2 = new JButton("英译中");
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				B = true;
			}
		});

		btnNewButton_2.setBounds(206, 11, 113, 27);
		frame.getContentPane().add(btnNewButton_2);
	}
}
